package com.becca.cake.controller.system;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.model.system.DeptDO;
import com.becca.cake.service.system.IUserDeptService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.PMSConstants;
import com.becca.cake.util.StringUtil;

/**
 * 部门管理
 * @author hejinguo
 * @version $Id: UserDeptAction.java, v 0.1 2013-12-2 上午12:35:11
 */
@Controller
@RequestMapping("/systemDept")
public class UserDeptAction extends BaseAction {
    private final static Log logger = LogFactory.getLog(UserDeptAction.class);
    @Autowired
    private IUserDeptService iUserDeptService;

    /**
     * 根据部门ID查询对应的部门树
     * @author hejinguo
     * @param deptRoot
     * @return
     */
    @RequestMapping(value = "/selectDeptTreeById", method = RequestMethod.POST)
    public @ResponseBody
    String selectDeptTreeById(@RequestParam(value = "deptRoot", required = false) String deptRoot) {
        Long deptId = Long.valueOf(PMSConstants.DEPT_ROOT);
        if (StringUtil.isNotEmpty(deptRoot)) {
            deptId = Long.valueOf(deptRoot);
        }
        try {
            return this.iUserDeptService.selectDeptTreeById(deptId);
        } catch (Exception e) {
            logger.error("查询部门信息时异常!", e);
        }
        return null;
    }

    /**
     * 根据ID查询对应的部门信息
     * @author hejinguo
     * @param deptId
     * @return
     */
    @RequestMapping(value = "/selectDeptMsgById", method = RequestMethod.POST)
    public @ResponseBody
    String selectDeptMsgById(@RequestParam(value = "deptId", required = false) String deptId) {
        if (StringUtil.isNotEmpty(deptId)) {
            try {
                return this.iUserDeptService.selectDeptMsgById(deptId);
            } catch (Exception e) {
                logger.error("部门管理中根据ID查询部门信息失败!", e);
                return JsonUtil.returnJsonUnSuccess("部门管理中根据ID查询部门信息失败!");
            }
        } else {
            logger.error("查询部门信息时ID不能为空!");
            return JsonUtil.returnJsonUnSuccess("查询部门信息时ID不能为空!");
        }
    }

    /**
     * 添加部门信息
     * @author hejinguo
     * @param functionDO
     * @return
     */
    @RequestMapping(value = "/insetDept", method = RequestMethod.POST)
    public @ResponseBody
    String insetDept(@ModelAttribute DeptDO deptDO) {
        if (deptDO != null) {
            deptDO.setCreatedMan(this.getUserSeesion().getTrueName());
            try {
                this.iUserDeptService.insetDept(deptDO);
                return JsonUtil.returnJsonSuccess("添加成功!");
            } catch (Exception e) {
                logger.error("添加失败!", e);
                return JsonUtil.returnJsonUnSuccess("添加失败!");
            }
        } else {
            logger.error("添加部门信息时信息为空!");
            return JsonUtil.returnJsonUnSuccess("添加部门信息时信息为空!");
        }
    }

    /**
     * 修改部门信息
     * @author hejinguo
     * @param functionDO
     * @return
     */
    @RequestMapping(value = "/updateDeptById", method = RequestMethod.POST)
    public @ResponseBody
    String updateDeptById(@ModelAttribute DeptDO deptDO) {
        if (deptDO != null) {
            deptDO.setUpdatedMan(this.getUserSeesion().getTrueName());
            try {
                this.iUserDeptService.updateDeptById(deptDO);
                return JsonUtil.returnJsonSuccess("修改成功!");
            } catch (Exception e) {
                logger.error("修改失败!", e);
                return JsonUtil.returnJsonUnSuccess("修改失败!");
            }
        } else {
            logger.error("修改部门信息时信息为空!");
            return JsonUtil.returnJsonUnSuccess("修改部门信息时信息为空!");
        }
    }
}
